﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using Eniux.Windows.ThreeDEngine.Helper;
using System.Windows;

namespace Eniux.Windows.ThreeDEngine.Visual3Ds
{
    /// <summary>
    /// 接受命中测试的方体
    /// </summary>
    public class BoxVisual3D : HitMeshElement3D
    {
        #region 注册属性

        #region Center

        /// <summary>
        /// Center属性.
        /// </summary>
        public static readonly DependencyProperty CenterProperty = DependencyProperty.Register(
            "Center", typeof(Point3D), typeof(BoxVisual3D), new UIPropertyMetadata(new Point3D(), GeometryChanged));


        /// <summary>
        /// 获取或设置方体中心点的3D坐标
        /// </summary>
        public Point3D Center
        {
            get{return (Point3D)GetValue(CenterProperty);}
            set{SetValue(CenterProperty, value);}
        }

        #endregion

        #region Length

        /// <summary>
        /// 长度属性
        /// </summary>
        public static readonly DependencyProperty LengthProperty = DependencyProperty.Register(
            "Length", typeof(double), typeof(BoxVisual3D), new UIPropertyMetadata(1.0, GeometryChanged));

        /// <summary>
        /// 获取或设置方体的长度
        /// </summary>
        public double Length
        {
            get{return (double)GetValue(LengthProperty);}
            set{SetValue(LengthProperty, value);}
        } 

        #endregion

        #region Width

        /// <summary>
        /// 宽度属性
        /// </summary>
        public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(
            "Width", typeof(double), typeof(BoxVisual3D), new UIPropertyMetadata(1.0, GeometryChanged));

        /// <summary>
        /// 获取或设置方体的宽度
        /// </summary>
        public double Width
        {
            get{return (double)GetValue(WidthProperty);}
            set{SetValue(WidthProperty, value);}
        }

        #endregion

        #region Height

        /// <summary>
        /// 高度属性
        /// </summary>
        public static readonly DependencyProperty HeightProperty = DependencyProperty.Register(
            "Height", typeof(double), typeof(BoxVisual3D), new UIPropertyMetadata(1.0, GeometryChanged));


        /// <summary>
        /// 获取或设置方体的高度
        /// </summary>
        public double Height
        {
            get{return (double)GetValue(HeightProperty);}
            set{ SetValue(HeightProperty, value);}
        }

        #endregion

        #region IsAddTopFace

        /// <summary>
        /// 是否添加方体上表面属性.
        /// </summary>
        public static readonly DependencyProperty IsAddTopFaceProperty = DependencyProperty.Register(
            "IsAddTopFace", typeof(bool), typeof(BoxVisual3D), new UIPropertyMetadata(true, GeometryChanged));

        /// <summary>
        /// 获取或设置是否显示方体的上表面
        /// </summary>
        public bool IsAddTopFace
        {
            get{return (bool)GetValue(IsAddTopFaceProperty);}
            set{SetValue(IsAddTopFaceProperty, value);}
        } 

        #endregion
       
        #region IsAddBottomFace

        /// <summary>
        /// 是否添加方体下表面属性
        /// </summary>
        public static readonly DependencyProperty IsAddBottomFaceProperty = DependencyProperty.Register(
            "IsAddBottomFace", typeof(bool), typeof(BoxVisual3D), new UIPropertyMetadata(true, GeometryChanged));
        
        /// <summary>
        /// 获取或者设置方体是否显示下表面
        /// </summary>
        public bool IsAddBottomFace
        {
            get{return (bool)GetValue(IsAddBottomFaceProperty);}
            set{SetValue(IsAddBottomFaceProperty, value);}
        }

        #endregion

        #endregion

        #region 方法

        /// <summary>
        ///生成 MeshGeometry3D
        /// </summary>
        /// <returns></returns>
        protected override MeshGeometry3D Tessellate()
        {
            var b = new MeshBuilder();
            b.AddCubeFace(Center, new Vector3D(-1, 0, 0), new Vector3D(0, 0, 1), Length, Width, Height);
            b.AddCubeFace(Center, new Vector3D(1, 0, 0), new Vector3D(0, 0, -1), Length, Width, Height);
            b.AddCubeFace(Center, new Vector3D(0, -1, 0), new Vector3D(0, 0, 1), Width, Length, Height);
            b.AddCubeFace(Center, new Vector3D(0, 1, 0), new Vector3D(0, 0, -1), Width, Length, Height);
            if (IsAddTopFace)
            {
                b.AddCubeFace(Center, new Vector3D(0, 0, 1), new Vector3D(0, -1, 0), Height, Length, Width);
            }
            if (IsAddBottomFace)
            {
                b.AddCubeFace(Center, new Vector3D(0, 0, -1), new Vector3D(0, 1, 0), Height, Length, Width);
            }
            return b.ToMesh();
        }

        #endregion
    }
}
